package com.java110.mall.goods.smo.impl;


import com.java110.core.base.smo.BaseServiceSMO;
import com.java110.dto.PageDto;
import com.java110.dto.distributionMode.DistributionModeDto;
import com.java110.dto.product.ProductDto;
import com.java110.dto.product.ProductSpecDetailDto;
import com.java110.dto.product.ProductSpecValueDto;
import com.java110.intf.goods.IProductInnerServiceSMO;
import com.java110.mall.goods.dao.IDistributionModeServiceDao;
import com.java110.mall.goods.dao.IProductServiceDao;
import com.java110.mall.goods.dao.IProductSpecDetailServiceDao;
import com.java110.po.product.ProductPo;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.DateUtil;
import com.java110.utils.util.ListUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName FloorInnerServiceSMOImpl
 * @Description 产品内部服务实现类
 * @Author wuxw
 * @Date 2019/4/24 9:20
 * @Version 1.0
 * add by wuxw 2019/4/24
 **/
@RestController
public class ProductInnerServiceSMOImpl extends BaseServiceSMO implements IProductInnerServiceSMO {

    @Autowired
    private IProductServiceDao productServiceDaoImpl;

    @Autowired
    private IProductSpecDetailServiceDao productSpecDetailServiceDaoImpl;
    @Autowired
    private IDistributionModeServiceDao distributionModeServiceDaoImpl;

    @Override
    public int saveProduct(@RequestBody ProductPo productPo) {
        int saveFlag = 1;
        productServiceDaoImpl.saveProductInfo(BeanConvertUtil.beanCovertMap(productPo));
        return saveFlag;
    }

    @Override
    public int updateProduct(@RequestBody ProductPo productPo) {
        int saveFlag = 1;
        productServiceDaoImpl.updateProductInfo(BeanConvertUtil.beanCovertMap(productPo));
        return saveFlag;
    }

    @Override
    public int deleteProduct(@RequestBody ProductPo productPo) {
        int saveFlag = 1;
        productPo.setStatusCd("1");
        productServiceDaoImpl.updateProductInfo(BeanConvertUtil.beanCovertMap(productPo));
        return saveFlag;
    }

    @Override
    public int queryProductsCount(@RequestBody ProductDto productDto) {
        Map productMap = BeanConvertUtil.beanCovertMap(productDto);
        if (null != productDto.getCreateTime() && !"".equals(productDto.getCreateTime())) {
            productMap.put("createTime", DateUtil.getFormatTimeString(productDto.getCreateTime(), DateUtil.DATE_FORMATE_STRING_B));
        }
        return productServiceDaoImpl.queryProductsCount(productMap);
    }

    @Override
    public List<ProductDto> queryProducts(@RequestBody ProductDto productDto) {

        //校验是否传了 分页信息

        int page = productDto.getPage();

        if (page != PageDto.DEFAULT_PAGE) {
            productDto.setPage((page - 1) * productDto.getRow());
        }
        List<ProductDto> products = new ArrayList<>();
        Map productMap = BeanConvertUtil.beanCovertMap(productDto);
        if (null != productDto.getCreateTime() && !"".equals(productDto.getCreateTime())) {
            productMap.put("createTime", DateUtil.getFormatTimeString(productDto.getCreateTime(), DateUtil.DATE_FORMATE_STRING_B));
        }
        List<Map> prods = productServiceDaoImpl.getProductInfo(productMap);
        ProductDto tmpProductDto = null;
        ProductSpecValueDto productSpecValueDto = null;
        List<String> specIds = new ArrayList<>();
//        List<String> modeIds = new ArrayList<>();
        for (Map prod : prods) {
            tmpProductDto = BeanConvertUtil.covertBean(prod, ProductDto.class);
            productSpecValueDto = BeanConvertUtil.covertBean(prod, ProductSpecValueDto.class);
            specIds.add(productSpecValueDto.getSpecId());
//            modeIds.add(tmpProductDto.getModeId());
            tmpProductDto.setDefaultSpecValue(productSpecValueDto);
            products.add(tmpProductDto);
        }
//        refreshDistributionMode(products,modeIds);

        return products;
    }

    private void refreshDistributionMode(List<ProductDto> products, List<String> modeIds) {
        if (products.size() < 1) {
            return;
        }
        if (modeIds.size() < 1) {
            return;
        }
        // 刷入规格 值信息
        DistributionModeDto distributionModeDto = new DistributionModeDto();
        distributionModeDto.setModeIds(modeIds.toArray(new String[modeIds.size()]));
        distributionModeDto.setShopId(products.get(0).getShopId());
        List<DistributionModeDto> distributionModeDtos = BeanConvertUtil.covertBeanList(
                distributionModeServiceDaoImpl.getDistributionModeInfo(BeanConvertUtil.beanCovertMap(distributionModeDto)),
                DistributionModeDto.class);

        List<DistributionModeDto> tmpDistributionModeDtos = null;
        for (ProductDto tmpProduct : products) {
            tmpDistributionModeDtos = new ArrayList<>();
            for (DistributionModeDto tmpDistributionModeDto : distributionModeDtos) {
                if (tmpProduct.getModeId().equals(tmpDistributionModeDto.getModeId())) {
                    tmpDistributionModeDtos.add(tmpDistributionModeDto);
                }
            }
            tmpProduct.setDistributionModeDtos(tmpDistributionModeDtos);
        }
    }

    @Override
    public List<ProductDto> searchProducts(@RequestBody ProductDto productDto) {

        //校验是否传了 分页信息

        int page = productDto.getPage();

        if (page != PageDto.DEFAULT_PAGE) {
            productDto.setPage((page - 1) * productDto.getRow());
        }
        List<ProductDto> products = new ArrayList<>();
        List<Map> prods = productServiceDaoImpl.searchProducts(BeanConvertUtil.beanCovertMap(productDto));
        ProductDto tmpProductDto = null;
        ProductSpecValueDto productSpecValueDto = null;
        for (Map prod : prods) {
            tmpProductDto = BeanConvertUtil.covertBean(prod, ProductDto.class);
            productSpecValueDto = BeanConvertUtil.covertBean(prod, ProductSpecValueDto.class);

            tmpProductDto.setDefaultSpecValue(productSpecValueDto);
            products.add(tmpProductDto);
        }

        return products;
    }


    @Override
    public List<ProductDto> queryCashierProductDetail(@RequestBody ProductDto productDto) {

        //校验是否传了 分页信息

        int page = productDto.getPage();

        if (page != PageDto.DEFAULT_PAGE) {
            productDto.setPage((page - 1) * productDto.getRow());
        }
        List<ProductDto> products = new ArrayList<>();
        Map productMap = BeanConvertUtil.beanCovertMap(productDto);
        if (null != productDto.getCreateTime() && !"".equals(productDto.getCreateTime())) {
            productMap.put("createTime", DateUtil.getFormatTimeString(productDto.getCreateTime(), DateUtil.DATE_FORMATE_STRING_B));
        }
        List<Map> prods = productServiceDaoImpl.getProductDetailInfo(productMap);
        ProductDto tmpProductDto = null;
        ProductSpecValueDto productSpecValueDto = null;
        List<String> specIds = new ArrayList<>();
        for (Map prod : prods) {
            tmpProductDto = BeanConvertUtil.covertBean(prod, ProductDto.class);
            productSpecValueDto = BeanConvertUtil.covertBean(prod, ProductSpecValueDto.class);
            tmpProductDto.setDefaultSpecValue(productSpecValueDto);
            specIds.add(productSpecValueDto.getSpecId());
            products.add(tmpProductDto);
        }

        if (specIds.size() < 1) {
            return products;
        }

        // 刷入规格 值信息
        ProductSpecDetailDto productSpecDetailDto = new ProductSpecDetailDto();
        productSpecDetailDto.setSpecIds(specIds.toArray(new String[specIds.size()]));
        productSpecDetailDto.setStoreId(productDto.getStoreId());
        List<ProductSpecDetailDto> productSpecDetailDtos = BeanConvertUtil.covertBeanList(
                productSpecDetailServiceDaoImpl.getProductSpecDetailInfo(BeanConvertUtil.beanCovertMap(productSpecDetailDto)),
                ProductSpecDetailDto.class);
        List<ProductSpecDetailDto> tmpProductSpecDetailDtos = null;
        for (ProductDto tmpProduct : products) {
            tmpProductSpecDetailDtos = new ArrayList<>();
            for (ProductSpecDetailDto tmpProductSpecDetailDto : productSpecDetailDtos) {
                if (tmpProduct.getDefaultSpecValue().getSpecId().equals(tmpProductSpecDetailDto.getSpecId())) {
                    tmpProductSpecDetailDtos.add(tmpProductSpecDetailDto);
                }
            }
            tmpProduct.getDefaultSpecValue().setProductSpecDetails(tmpProductSpecDetailDtos);
        }

        return products;
    }


    @Override
    public List<ProductDto> queryHotProduct(@RequestBody ProductDto productDto) {
        Map productMap = BeanConvertUtil.beanCovertMap(productDto);
        productMap.put("createTime", DateUtil.getFormatTimeString(productDto.getCreateTime(), DateUtil.DATE_FORMATE_STRING_B));
        List<Map> maps = productServiceDaoImpl.queryHotProduct(productMap);
        List<ProductDto> productDtos = BeanConvertUtil.covertBeanList(maps, ProductDto.class);

        return productDtos;
    }

    @Override
    public int queryProductAndSpecCount(@RequestBody ProductDto productDto) {
        return productServiceDaoImpl.queryProductAndSpecCount(BeanConvertUtil.beanCovertMap(productDto));
    }

    @Override
    public List<ProductDto> queryProductAndSpecs(@RequestBody ProductDto productDto) {
        //校验是否传了 分页信息

        int page = productDto.getPage();

        if (page != PageDto.DEFAULT_PAGE) {
            productDto.setPage((page - 1) * productDto.getRow());
        }
        List<ProductDto> products = new ArrayList<>();
        Map productMap = BeanConvertUtil.beanCovertMap(productDto);
        List<Map> prods = productServiceDaoImpl.queryProductAndSpecsInfo(productMap);
        ProductDto tmpProductDto = null;
        ProductSpecValueDto productSpecValueDto = null;
        List<String> specIds = new ArrayList<>();
        for (Map prod : prods) {
            tmpProductDto = BeanConvertUtil.covertBean(prod, ProductDto.class);
            productSpecValueDto = BeanConvertUtil.covertBean(prod, ProductSpecValueDto.class);
            specIds.add(productSpecValueDto.getSpecId());
            tmpProductDto.setDefaultSpecValue(productSpecValueDto);
            productSpecValueDto.setSpecName(tmpProductDto.getSpecName());
            products.add(tmpProductDto);
        }

        if (ListUtil.isNull(specIds)) {
            return products;
        }

        // 刷入规格 值信息
        ProductSpecDetailDto productSpecDetailDto = new ProductSpecDetailDto();
        productSpecDetailDto.setSpecIds(specIds.toArray(new String[specIds.size()]));
        productSpecDetailDto.setStoreId(productDto.getStoreId());
        List<ProductSpecDetailDto> productSpecDetailDtos = BeanConvertUtil.covertBeanList(
                productSpecDetailServiceDaoImpl.getProductSpecDetailInfo(BeanConvertUtil.beanCovertMap(productSpecDetailDto)),
                ProductSpecDetailDto.class);
        List<ProductSpecDetailDto> tmpProductSpecDetailDtos = null;
        String specName = "";
        for (ProductDto tmpProduct : products) {
            tmpProductSpecDetailDtos = new ArrayList<>();
            specName = "";
            for (ProductSpecDetailDto tmpProductSpecDetailDto : productSpecDetailDtos) {
                if (tmpProduct.getDefaultSpecValue().getSpecId().equals(tmpProductSpecDetailDto.getSpecId())) {
                    tmpProductSpecDetailDtos.add(tmpProductSpecDetailDto);
                    specName += (tmpProductSpecDetailDto.getDetailValue() + "/");
                }
            }
            specName = specName.endsWith("/") ? specName.substring(0, specName.length() - 1) : specName;
            tmpProduct.getDefaultSpecValue().setProductSpecDetails(tmpProductSpecDetailDtos);
            if(StringUtils.isNotBlank(specName)) {
                tmpProduct.getDefaultSpecValue().setSpecName(specName);
            }
        }

        return products;
    }

    @Override
    public int queryGroupProductsCount(@RequestBody ProductDto productDto) {
        Map productMap = BeanConvertUtil.beanCovertMap(productDto);
        if (null != productDto.getCreateTime() && !"".equals(productDto.getCreateTime())) {
            productMap.put("createTime", DateUtil.getFormatTimeString(productDto.getCreateTime(), DateUtil.DATE_FORMATE_STRING_B));
        }
        return productServiceDaoImpl.queryGroupProductsCount(productMap);
    }

    @Override
    public List<ProductDto> queryGroupProducts(@RequestBody ProductDto productDto) {

        //校验是否传了 分页信息

        int page = productDto.getPage();

        if (page != PageDto.DEFAULT_PAGE) {
            productDto.setPage((page - 1) * productDto.getRow());
        }
        List<ProductDto> products = new ArrayList<>();
        Map productMap = BeanConvertUtil.beanCovertMap(productDto);
        if (null != productDto.getCreateTime() && !"".equals(productDto.getCreateTime())) {
            productMap.put("createTime", DateUtil.getFormatTimeString(productDto.getCreateTime(), DateUtil.DATE_FORMATE_STRING_B));
        }
        List<Map> prods = productServiceDaoImpl.getGroupProductInfo(productMap);
        ProductDto tmpProductDto = null;
        ProductSpecValueDto productSpecValueDto = null;
        List<String> specIds = new ArrayList<>();
//        List<String> modeIds = new ArrayList<>();
        for (Map prod : prods) {
            tmpProductDto = BeanConvertUtil.covertBean(prod, ProductDto.class);
            productSpecValueDto = BeanConvertUtil.covertBean(prod, ProductSpecValueDto.class);
            specIds.add(productSpecValueDto.getSpecId());
//            modeIds.add(tmpProductDto.getModeId());
            tmpProductDto.setDefaultSpecValue(productSpecValueDto);
            products.add(tmpProductDto);
        }
//        refreshDistributionMode(products,modeIds);

        return products;
    }

    @Override
    public int querySeckillProductsCount(@RequestBody ProductDto productDto) {
        Map productMap = BeanConvertUtil.beanCovertMap(productDto);
        if (null != productDto.getCreateTime() && !"".equals(productDto.getCreateTime())) {
            productMap.put("createTime", DateUtil.getFormatTimeString(productDto.getCreateTime(), DateUtil.DATE_FORMATE_STRING_B));
        }
        return productServiceDaoImpl.querySeckillProductsCount(productMap);
    }

    @Override
    public List<ProductDto> querySeckillProducts(@RequestBody ProductDto productDto) {

        //校验是否传了 分页信息

        int page = productDto.getPage();

        if (page != PageDto.DEFAULT_PAGE) {
            productDto.setPage((page - 1) * productDto.getRow());
        }
        List<ProductDto> products = new ArrayList<>();
        Map productMap = BeanConvertUtil.beanCovertMap(productDto);
        if (null != productDto.getCreateTime() && !"".equals(productDto.getCreateTime())) {
            productMap.put("createTime", DateUtil.getFormatTimeString(productDto.getCreateTime(), DateUtil.DATE_FORMATE_STRING_B));
        }
        List<Map> prods = productServiceDaoImpl.getSeckillProductInfo(productMap);
        ProductDto tmpProductDto = null;
        ProductSpecValueDto productSpecValueDto = null;
        List<String> specIds = new ArrayList<>();
//        List<String> modeIds = new ArrayList<>();
        for (Map prod : prods) {
            tmpProductDto = BeanConvertUtil.covertBean(prod, ProductDto.class);
            productSpecValueDto = BeanConvertUtil.covertBean(prod, ProductSpecValueDto.class);
            specIds.add(productSpecValueDto.getSpecId());
//            modeIds.add(tmpProductDto.getModeId());
            tmpProductDto.setDefaultSpecValue(productSpecValueDto);
            products.add(tmpProductDto);
        }
//        refreshDistributionMode(products,modeIds);

        return products;
    }

    public IProductServiceDao getProductServiceDaoImpl() {
        return productServiceDaoImpl;
    }

    public void setProductServiceDaoImpl(IProductServiceDao productServiceDaoImpl) {
        this.productServiceDaoImpl = productServiceDaoImpl;
    }
}
